package fch;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests that robots using the MovingRobot class work as intended and that the moveToPoint methods
 * move the robot to the point within a reasonable amount of error.
 * @author Christopher Foo
 *
 */
public class TestMovingRobot extends RobotTestBed {
  
  /** True if the MovingRobotTester visited the center. */
  private boolean moveCenter = false;
  
  /** True if the MovingRobotTester visited the bottom-right corner. */
  private boolean moveCorner = false;
  
  /** True if the MovingRobotTester correctly handled a point outside of the battlefield. */
  private boolean moveOutside = false;
  
  @Override
  /**
   * Check to see if the MovingRobotTester visited the specified spots as intended.
   * 
   * @param event Details about the finished turn.
   */
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1]; //The MovingRobotTester robot.
    double x = robot.getX(); //The X coordinate of the robot at the end of the turn.
    double y = robot.getY(); //The Y coordinate of the robot at the end of the turn.
    final int allowableError = 18; //Allowable difference to account for the robot's dimensions.
    
    //Check if in center
    if ((x >= width / 2 - allowableError || x <= width / 2 + allowableError) 
        && (y >= height / 2 - allowableError || y <= height / 2 + allowableError)) {
      moveCenter = true;
    }
    
    //Check if in bottom-right corner
    if ((x >= width - allowableError || x <= width) 
        && (y >= 0 || y <= allowableError)) {
      moveCorner = true;
    }
    
    //Check when told to move outside
    if ((x >= width / 2 - allowableError || x <= width / 2 + allowableError) 
        && (y >= height - allowableError || y <= height )) {
      moveOutside = true;
    }
  }
  
  /**
   * After the battle, check to see that all the specified spots were visited.
   *
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Check Center", moveCenter);
    assertTrue("Check Bottom-Right", moveCorner);
    assertTrue("Check Outside", moveOutside);

  }
  
  @Override
  /**
   * Use SittingDuck and the MovingRobotTester for this test.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  public String getRobotNames() {
    return "sample.SittingDuck,fch.MovingRobotTester";
  }

  /**
   * This test runs for 10 round.
   *
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }


}
